import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * 绘图区
 */
public class MyPanel extends JPanel implements KeyListener,Runnable {
    //定义坦克
    Hero hero = null;
    Bullet bullet = null;
    //定义敌方坦克，存放到Vector
    Vector<enemyTank> enemyTanks = new Vector<>();
    //敌方坦克数量
    int enemySize = 3;
    //存放炸弹
    //当击中坦克，调用一次boom
    Vector<Boom> booms = new Vector<>();

    //定义三张爆炸图片，用于显示爆炸效果
    Image i1 = null;
    Image i2 = null;
    Image i3 = null;


    public MyPanel(){
        hero = new Hero(100,100);//初始化坦克
        hero.setSpeed(10);
        //初始化敌方位置
        for (int i = 0; i < enemySize; i++ ){
            enemyTank enemytank = new enemyTank((200 * (i+1)),50);
            enemytank.setDirect(2);
            new Thread(enemytank).start();
            enemyTanks.add(enemytank);
        }
        //初始爆炸图片对象
        i1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
        i2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
        i3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));



    }


    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0,0,1000 ,750);//填充矩形
        if (hero.isLive) {
            drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0);

        }

        //画出敌方坦克，遍历Vector
        for (int i = 0; i < enemyTanks.size(); i++){
            enemyTank emetank = enemyTanks.get(i);
            if (emetank.isLive){
                drawTank(emetank.getX(), emetank.getY(),g,emetank.getDirect(),1);
                //敌方坦克发射子弹
                for (int j = 0; j < enemyTanks.get(i).vector.size();j++){
                    if (enemyTanks.get(i).bullet != null && enemyTanks.get(i).bullet.isLive != false){
                        g.setColor(Color.white);
                        g.fillOval(enemyTanks.get(i).vector.get(j).x,enemyTanks.get(i).vector.get(j).y,15,15);
                    }else{
                        enemyTanks.get(i).vector.remove(enemyTanks.get(i).bullet);
                    }
                }
            }else{
                enemyTanks.remove(emetank);
            }
        }
        //画出我方坦克子弹
        for (int i = 0; i < hero.vector.size(); i++){
            Bullet b = hero.vector.get(i);
            if (b != null && b.isLive != false){
                g.setColor(Color.white);
                g.fillOval(b.x,b.y,15,15);
            }else{
                hero.vector.remove(b);
            }
        }
        //爆炸效果
        for (int i = 0; i < booms.size();i++){
            Boom bo = booms.get(i);
            if (bo.life>9){
                g.drawImage(i1,bo.x,bo.y,60,60,this);
            }else if (bo.life > 6){
                g.drawImage(i2,bo.x,bo.y,60,60,this);
            }else if (bo.life > 3){
                g.drawImage(i3,bo.x,bo.y,60,60,this);
            }
            bo.lifeDowm();
            if (bo.life == 0){
                booms.remove(bo);
            }
        }


    }
    //敌方坦克被击中
     public  void hitTank(Bullet b, enemyTank e){
        switch (e.getDirect()){
            case 0:
            case 2:
                if (b.x >= e.getX() && b.x <= e.getX()+40 &&
                b.y >= e.getY() && b.y <= e.getY() + 60){
                    b.isLive = false;
                    e.isLive = false;
                    //爆炸
                    Boom boom = new Boom(e.getX(),e.getY());
                    booms.add(boom);
                }
                break;
            case 1:
            case 3:
                if (b.x >= e.getX() && b.x <= e.getX()+60 &&
                        b.y >= e.getY() && b.y <= e.getY() + 40){
                    b.isLive = false;
                    e.isLive = false;
                    Boom boom = new Boom(e.getX(),e.getY());
                    booms.add(boom);
                }
                break;
        }
     }

    public  void hitHero(Bullet b, Hero e){
        switch (e.getDirect()){
            case 0:
            case 2:
                if (b.x > e.getX() && b.x < e.getX()+40 &&
                        b.y > e.getY() && b.y < e.getY() + 60){
                    b.isLive = false;
                    e.isLive = false;
                    //爆炸
                    Boom boom = new Boom(e.getX(),e.getY());
                    booms.add(boom);
                }
                break;
            case 1:
            case 3:
                if (b.x > e.getX() && b.x < e.getX()+60 &&
                        b.y > e.getY() && b.y < e.getY() + 40){
                    b.isLive = false;
                    e.isLive = false;
                    Boom boom = new Boom(e.getX(),e.getY());
                    booms.add(boom);
                }
                break;
        }
    }


    /**
     *
     * @param x 坦克的左上角x坐标
     * @param y 坦克的左上角y坐标
     * @param g 画笔
     * @param direct 坦克方向
     * @param type  坦克类型
     */
    public void drawTank(int x,int y,Graphics g,int direct,int type){
        //根据不同类型，设置不同颜色
        switch(type){
            //我方的坦克
            case 0:
                g.setColor(Color.CYAN);
                break;
            //敌方坦克
            case 1:
                g.setColor(Color.yellow);
                break;
        }



        switch (direct){
            case 0:
                g.fill3DRect(x, y,10,60,false);//坦克左边
                g.fill3DRect(x+30, y,10,60,false);//坦克右边
                g.fill3DRect(x+10, y+10,20,40,false);//中间部分‘
                g.fillOval(x+10, y+20,19,20);//圆盖
                g.drawLine(x+18,y,x+18,y+30); //炮管
                g.drawLine(x+22,y,x+22,y+30); //炮管
                break;
            case 1:
                g.fill3DRect(x, y,60,10,false);//坦克左边
                g.fill3DRect(x, y+30,60,10,false);//坦克右边
                g.fill3DRect(x+10, y+10,40,20,false);//中间部分‘
                g.fillOval(x+20, y+10,20,20);//圆盖
                g.drawLine(x+30,y+18,x+60,y+18); //炮管
                g.drawLine(x+30,y+22,x+60,y+22); //炮管
                break;
            case 2:
                g.fill3DRect(x, y,10,60,false);//坦克左边
                g.fill3DRect(x+30, y,10,60,false);//坦克右边
                g.fill3DRect(x+10, y+10,20,40,false);//中间部分‘
                g.fillOval(x+10, y+20,19,20);//圆盖
                g.drawLine(x+18,y+30,x+18,y+60); //炮管
                g.drawLine(x+22,y+30,x+22,y+60); //炮管
                break;
            case 3:
                g.fill3DRect(x, y,60,10,false);//坦克左边
                g.fill3DRect(x, y+30,60,10,false);//坦克右边
                g.fill3DRect(x+10, y+10,40,20,false);//中间部分‘
                g.fillOval(x+20, y+10,20,20);//圆盖
                g.drawLine(x,y+18,x+30,y+18); //炮管
                g.drawLine(x,y+22,x+30,y+22); //炮管
                break;
            default:
                System.out.println("暂时没有处理");
        }

    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int y = hero.getY();
        int x = hero.getX();
        switch (e.getKeyCode()){
            case KeyEvent.VK_W:
                if (hero.getY() > 0){
                    hero.setDirect(0);
                    hero.moveUp();
                }
                break;
            case KeyEvent.VK_S:
                if (hero.getY() + 60<750){
                    hero.setDirect(2);
                    hero.moveDown();
                }
                break;
            case KeyEvent.VK_A:
                if (hero.getX()>0){
                    hero.setDirect(3);
                    hero.moveLeft();
                }
                break;
            case KeyEvent.VK_D:
                if (hero.getX()+60 < 1000){
                    hero.setDirect(1);
                    hero.moveRight();
                }
                break;
            case KeyEvent.VK_J:
                    hero.shotEnemyTank();
                break;

        }


        this.repaint();

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    /**
     * 刷新屏幕
     */
    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int j = 0; j < hero.vector.size();j++){
                //判断是否击中敌方坦克
                Bullet bu = hero.vector.get(j);
                if (bu!=null && bu.isLive ){
                    for (int i = 0; i < enemyTanks.size(); i++){
                        enemyTank enemy = enemyTanks.get(i);
                        hitTank(hero.bullet,enemy);
                    }
                }
            }

            //判断我方是否被击中
            for (int i =  0; i < enemyTanks.size();i++){
                if (enemyTanks.get(i).bullet!=null && enemyTanks.get(i).bullet.isLive ){
                        Hero h = hero;
                        hitHero(enemyTanks.get(i).bullet,h);
                }
            }

//            //判断我方是否被击中
//            for (int i =  0; i < enemyTanks.size();i++){
//                enemyTank e = enemyTanks.get(i);
//                for(int j = 0; j < e.vector.size();i++){
//                    Bullet b = e.vector.get(j);
//                    if (hero.isLive  && b.isLive ){
//                        hitHero(b,hero);
//                    }
//                }
//
//            }

            this.repaint();
        }
    }
}
